package com.compass.ui.client;
import java.util.ArrayList;

import com.compass.ui.client.data.TestData;
import com.compass.ui.client.model.IndicModel;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.event.ChartEvent;
import com.extjs.gxt.charts.client.event.ChartListener;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.LineDataProvider;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.charts.AreaChart;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.charts.client.model.charts.LineChart;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.NumberPropertyEditor;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CellSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Window;
  
public class AdvancedChartExample extends LayoutContainer {  
  
  private EditorGrid<IndicModel> indicGrid;  
  private Radio selRadio;  
  private ContentPanel tablePanel;
  private final ListStore<IndicModel> store;
  private BarChart bar;
  private LineChart line1;
  private LineChart line;
  private ChartModel model;
  
  public AdvancedChartExample()
  {
	  this(new ContentPanel());
  }
  
  public AdvancedChartExample(ContentPanel panel)
  {
	  this(panel, "TITRE");
  }
  
  public AdvancedChartExample(ContentPanel panel, String title)
  {
	  store = new ListStore<IndicModel>();
	  line1 = new LineChart();
	  line = new AreaChart();
	  tablePanel = panel;
	  model = new ChartModel(title,  
	            "font-size: 14px; font-family: Verdana; text-align: center;"); 
  }
  
  @Override  
  protected void onRender(Element parent, int index) {  
    super.onRender(parent, index);  
  
    store.add(TestData.getIndic());  
  
    String url = GWT.getHostPageBaseURL() + "resources/chart/open-flash-chart.swf";
    final Chart chart = new Chart(url);  
    ChartListener listener = new ChartListener() {  
      public void chartClick(ChartEvent ce) {  
        int row = ce.getChartConfig().getValues().indexOf(ce.getDataType());  
        int col = ce.getChartModel().getChartConfigs().indexOf(ce.getChartConfig()) + 1;  
        CellSelectionModel<IndicModel> csm = (CellSelectionModel<IndicModel>) indicGrid.getSelectionModel();  
        if (selRadio.getValue()) {  
        	csm.selectCell(row, col);  
        } else {  
          indicGrid.startEditing(row, col);  
        }  
      }  
    };  
  
    model.setBackgroundColour("#fefefe");  
    model.setLegend(new Legend(Position.TOP, true));  
    model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER); 
    
    Legend lg = new Legend(Position.RIGHT);
    model.setLegend(lg);
    
    bar = new BarChart(BarStyle.GLASS);  
    bar.setColour(getRandomColor());  
    BarDataProvider barProvider = new BarDataProvider("indice", "weekNumber");  
    barProvider.bind(store);  
    bar.setDataProvider(barProvider);  
    bar.addChartListener(listener);  
    model.addChartConfig(bar);
    
    line.setText("Objectif");
    line.setColour("#FF0000");  
    LineDataProvider lineProvider = new LineDataProvider("obj");  
    lineProvider.bind(store);  
    line.setDataProvider(lineProvider);  
    model.addChartConfig(line);
    
    line1.setText("??");
    line1.setColour("#000000");
    line1.addChartListener(listener);
    LineDataProvider line1Provider = new LineDataProvider("line");  
    line1Provider.bind(store);  
    line1.setDataProvider(line1Provider);  
    model.addChartConfig(line1);
    
    LineChart line2 = new LineChart();
    line2.setText("??");
    line2.setColour("#0000FF");
    LineDataProvider line2Provider = new LineDataProvider("line2");
    line2Provider.bind(store);
    line2.setDataProvider(line2Provider);
    model.addChartConfig(line2);
    
    chart.setChartModel(model);  
  
    // grid  
    NumberPropertyEditor npe = new NumberPropertyEditor(Integer.class);  
    ArrayList<ColumnConfig> cols = new ArrayList<ColumnConfig>();  
  
    ColumnConfig weekCol = new ColumnConfig("weekNumber", "Numero de semaine", 100);  
    cols.add(weekCol);  
    weekCol.setEditor(new CellEditor(new TextField<String>()));  
  
    ColumnConfig indiceCol = new ColumnConfig("indice", "Indice", 100);  
    cols.add(indiceCol);  
    NumberField nf = new NumberField();  
    nf.setPropertyEditor(npe);  
    indiceCol.setEditor(new CellEditor(nf));  
    
    ColumnConfig objCol = new ColumnConfig("obj", "Objectif", 100);
    cols.add(objCol);
    NumberField nfObj = new NumberField();
    nfObj = new NumberField();
    nfObj.setPropertyEditor(npe);
    objCol.setEditor(new CellEditor(nfObj));
    
    ColumnConfig lineCol = new ColumnConfig("line", "??", 100);
    cols.add(lineCol);
    NumberField nfLine = new NumberField();
    nfLine = new NumberField();
    nfLine.setPropertyEditor(npe);
    lineCol.setEditor(new CellEditor(nfLine));
  
    ColumnModel cm = new ColumnModel(cols);  
  
    indicGrid = new EditorGrid<IndicModel>(store, cm);  
    indicGrid.getView().setForceFit(true);  
    indicGrid.getView().setShowDirtyCells(false);  
    indicGrid.addListener(Events.AfterEdit, new Listener<GridEvent<IndicModel>>() {  
      public void handleEvent(GridEvent<IndicModel> be) {  
        store.commitChanges();  
      }  
    });  
  
    Button reload = new Button("Reload Team Sales Data");  
    reload.addSelectionListener(new SelectionListener<ButtonEvent>() {  
      @Override  
      public void componentSelected(ButtonEvent ce) {  
        store.removeAll();  
        store.add(TestData.getIndic());  
      }  
    });  
  
    RadioGroup rg = new RadioGroup();  
    rg.setFieldLabel("Chart click");  
    Radio edRadio = new Radio();  
    edRadio.setBoxLabel("Edits Cell");  
    rg.add(edRadio);  
    selRadio = new Radio();  
    selRadio.setValue(true);  
    selRadio.setBoxLabel("Selects Cell");  
    rg.add(selRadio);  
    LayoutContainer radForm = new LayoutContainer(new FormLayout(LabelAlign.RIGHT));  
    radForm.add(rg);  
  
    LayoutContainer lc = new LayoutContainer();  
    RowLayout rl = new RowLayout();  
    lc.setLayout(rl);  
    lc.setSize(800, 600);  
  
    RowData data;  
    data = new RowData(1, 300, new Margins(10));  
    ContentPanel cp = new ContentPanel(new FitLayout());  
    cp.setHeading("Chart");  
    cp.add(chart);  
    lc.add(cp, data);  

    tablePanel.add(indicGrid);  
  
    add(chart);  
  
  }  
  
  public void addLineListener(ChartListener listener)
  {
	  line.addChartListener(listener);
  }
  
  public void reload()
  {
	 store.removeAll();
	 store.add(TestData.getIndic());
	 bar.setColour(getRandomColor());
  }
  
  public static boolean isExplorer() {
	    String test = Window.Location.getPath();
	    if (test.indexOf("pages") != -1) {
	      return false;
	    }
	    return true;
	  }
  
  public static String getRandomColor()
  {
	  int i = Random.nextInt(3);
	  
	  switch (i)
	  {
	  case 0:
		
		return "#00aa00";
	  case 1 :
		  return "#EB0920";
		  
	  case 2 :
		  return "#093EEB";
	  default:
		return "#00aa00";
	}
  }
}  
